package com.sykj.springboot.common.entity;

import java.io.Serializable;

/**
 * @Auther: wuying
 * @Date: 2018-07-09 09:53
 *   json 返回参数封装含 格式  {"data":"{}","msg":"","status":"S"}
 *   status: S 表示调用成功，T 表示该用户未登陆或超时，或无权访问，F表示调用接口失败。
 */
public class JsonResult<T> implements Serializable {

    public static final String SUCCESS = "S";

    public static final String TIME_OUT = "T";

    public static final String ERROR = "F";
    public static final String IMPORT_ERROR = "IMF";
    /**
     * 	调用mis平台出现所有错误的状态
     */
    public static final String MIS_ERROR = "MF";
    //数据
    private T data;
    // 错误消息
    private String msg;
    // 错误编码
    private String status;

    public JsonResult() {
    }

    public JsonResult(String error){
        status = ERROR;
        this.msg = error;
    }

    public JsonResult(String msg, String status){
        this.status = status;
        this.msg = msg;
    }

    public JsonResult(T data){
        status = SUCCESS;
        this.data = data;
    }

    public JsonResult(Throwable e) {
        status = ERROR;
        msg = e.getMessage();
    }

    public JsonResult(String status, Throwable e) {
        this.status = status;
        this.msg = e.getMessage();
    }

    public T getData() {
        return data;
    }

    public void setData(T data) { this.data = data; }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        if(status.equals(SUCCESS)){
            this.msg = "成功!";
        }
        this.status = status;
    }

    @Override
    public String toString() {
        return "JsonResult [data=" + data + ", msg=" + msg + ", status=" + status + "]";
    }
}
